package com.eastfair.visitor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.visitor.dao.ReservationMapper;
import com.eastfair.visitor.entity.Department;
import com.eastfair.visitor.entity.Reservation;
import com.eastfair.visitor.service.ReservationService;
import com.eastfair.boot.service.SuperServiceImpl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import java.util.Collection;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
import com.eastfair.visitor.vo.*;
import com.eastfair.visitor.dto.*;
/**
* <p>
    * 业务实现类
    * 访客预约
    * </p>
*
* @author lyw
* @date 2022-11-18
*/
@Slf4j
@Service
//@DS("#thread.tenant")
    public class ReservationServiceImpl extends SuperServiceImpl<ReservationMapper, Reservation> implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
    */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<Reservation> modelList) {
    modelList.stream().forEach(s -> {
    s.setIsDeleted(BusinessConstant.YES);
    });
    // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
    return R.successDef();
    }

    /**
    * 通用查询
    *
    * @param modelDTO DTO
    * @param ids 主键id集合
    * @return list
    */
    @Override
    public List<Reservation> list(ReservationDTO modelDTO, List<Long> ids) {
        return ((ReservationService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids 主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
       magic = @Magic(
       key = "{"+
       "{'visitorName', #args[0]?.visitorName},"+
       "{'idcard', #args[0]?.idcard},"+
       "{'mobilePhone', #args[0]?.mobilePhone},"+
       "{'departmentId', #args[0]?.departmentId},"+
       "{'admissionTime', #args[0]?.admissionTime},"+
       "{'outTime', #args[0]?.outTime},"+
       "{'trip', #args[0]?.trip},"+
       "{'isRisk', #args[0]?.isRisk},"+
       "{'isExit', #args[0]?.isExit},"+
       "{'isEnabled', #args[0]?.isEnabled},"+
        "{'id', #args[1]}"+
        "}"))
    public List<Reservation> listOfCache(ReservationDTO modelDTO, List<Long> ids) {
       //查询
       QueryWrapper<Reservation> queryWrapper = new QueryWrapper<>();
       if (modelDTO != null) {
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getVisitorName()),Reservation::getVisitorName,modelDTO.getVisitorName());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getIdcard()),Reservation::getIdcard,modelDTO.getIdcard());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getMobilePhone()),Reservation::getMobilePhone,modelDTO.getMobilePhone());
                    queryWrapper.lambda().eq(modelDTO.getDepartmentId() != null,Reservation::getDepartmentId,modelDTO.getDepartmentId());
                    queryWrapper.lambda().eq(modelDTO.getAdmissionTime() != null,Reservation::getAdmissionTime,modelDTO.getAdmissionTime());
                    queryWrapper.lambda().eq(modelDTO.getOutTime() != null,Reservation::getOutTime,modelDTO.getOutTime());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getTrip()),Reservation::getTrip,modelDTO.getTrip());
                    queryWrapper.lambda().eq(modelDTO.getIsRisk() != null,Reservation::getIsRisk,modelDTO.getIsRisk());
                    queryWrapper.lambda().eq(modelDTO.getIsExit() != null,Reservation::getIsExit,modelDTO.getIsExit());
            }
            if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(Reservation::getId, ids);
            }
            queryWrapper.lambda().eq(Reservation::getIsDeleted, BusinessConstant.DELETE_NO);
            List<Reservation> modelList = list(queryWrapper);
            return modelList;
    }


    /**
     * 添加访客预约信息
     * @param reservationSaveDTO
     * @return
     */
    @Override
    public Boolean saveReservationInfo(ReservationSaveDTO reservationSaveDTO) {
        log.info("ReservationServiceImpl +++++ saveReservationInfo - 添加访客预约信息 reservationSaveDTO={}", JSONObject.toJSON(reservationSaveDTO));
        Reservation reservation = BeanUtil.toBean(reservationSaveDTO, Reservation.class);
        boolean save = save(reservation);
        return save;
    }


    /**
     * 通过id获取访客信息
     * @param reservationDTO
     * @return
     */
    @Override
    public ReservationVO getReservationInfoById(ReservationDTO reservationDTO) {
        log.info("ReservationServiceImpl +++++ getReservationInfoById - 通过id获取访客信息 reservationDTO={}", JSONObject.toJSON(reservationDTO));
        Reservation reservation = reservationMapper.selectById(reservationDTO.getId());
        ReservationVO reservationVO = BeanUtil.toBean(reservation, ReservationVO.class);
        return reservationVO;
    }


    /**
     * do转vo
     * @param model model
     * @return modelVo
     */
    @Override
    public ReservationVO doToVo(Reservation model) {
        ReservationVO modelVo = new ReservationVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }
    /**
     * do转vo
     * @param modelList modelList
     * @return list
     */
     @Override
     public List<ReservationVO> doToVos(List<Reservation> modelList) {
        if (modelList == null || modelList.isEmpty()) {
        return null;
        }
        List<ReservationVO> result = new ArrayList<>();
        for (Reservation model : modelList) {
           result.add(doToVo(model));
        }
        return result;
    }

    public void handlerQueryParams(PageParams<ReservationPageQuery> params) {
        if (StrUtil.isBlank(params.getSort()) || StrUtil.isBlank(params.getOrder())) {
            params.setSort("createTime");
            params.setOrder("descending");
        }
    }
}
